#!/usr/bin/perl

use strict;

print "Content-type: text/html\n\n";

default();

my ($buffer, $pairs, $key, $value, @keys, @values, $option, $cat, $sub, $command);

if ($ENV{'REQUEST_METHOD'} eq 'POST')
{
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

	# processes the information supplied by the POST CGI method
	foreach $pairs (split /&/, $buffer)
	{
	        ($key, $value) = split(/=/, $pairs);
	        $value =~ tr/+/ /;
	        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	        push(@keys, $key);
	        push(@values, $value);
	}	
	
	$option = pop @values;
	$cat = shift @values;
	$sub = shift @values;
	$command = "./adminrules ";

	if($option eq 'Add Cat.' && $cat ne '' && $sub eq '')
	{
		$command .= "1 $cat";
		print "$command";
		`$command`;
	}
	elsif ($option eq 'Add Sub.' && ($cat ne '' && $sub ne ''))
	{
		my $weight = shift @values;
		if ($weight =~ m/^[-+]?\d*\.?\d+$/)
		{
			$command .= "2 $cat $sub $weight";
			print "$command";
			`$command`;
		}
		else
                {
                        print "ERROR: Please Enter Correct Input!";
                }

	}
	elsif ($option eq 'Modify Sub. Weight' && ($cat ne '' && $sub ne ''))
	{
		my $weight = shift @values;
                if ($weight =~ m/^[-+]?\d*\.?\d+$/) #/^\d+\.?\d*$/)
                {
                        $command .= "3 $cat $sub $weight";
                        print "$command";
                       `$command`;
                }
		else 
		{
			print "ERROR: Please Enter Correct Input!";
		}
	}
	elsif ($option eq 'Add Phrase' && ($cat ne '' && $sub ne ''))
	{
		my $subweight = shift @values;
		my $phrase = shift @values;
		my $pweight = shift @values;
		
		if ($subweight eq '' && $phrase ne '' && $pweight =~ m/^[-+]?\d*\.?\d+$/)
		{
			$command .= "4 $cat $sub $phrase $pweight";
			print "$command";
			`$command`;
		}
		else 
                {
                        print "ERROR: Please Enter Correct Input!";
                }
	}
	elsif ($option eq 'Modify Phrase Weight' && ($cat ne '' && $sub ne ''))
	{
		my $subweight = shift @values;
                my $phrase = shift @values;
                my $pweight = shift @values;

                if ($subweight eq '' && $phrase ne '' && $pweight =~ m/^[-+]?\d*\.?\d+$/)
                {
                        $command .= "5 $cat $sub $phrase $pweight";
                        print "$command";
                       `$command`;
                }
		else
                {
                        print "ERROR: Please Enter Correct Input!";
                }

	}
	elsif ($option eq 'Remove Cat.' && $cat ne '' && $sub eq '')
	{
		$command .= "6 $cat";
		print "$command";
		`$command`;
	}
	elsif ($option eq 'Remove Sub.' && ($cat ne '' && $sub ne ''))
	{
		$command .= "7 $cat $sub";
		print "$command";
		`$command`;
	}
	elsif ($option eq 'Remove Phrase' && ($cat ne '' && $sub ne ''))
	{
		my $phrase = shift @values;
		if ($phrase ne '')
		{
			$command .= "8 $cat $sub $phrase";
			print "$command";
			`$command`;
		}
		else
                {
                        print "ERROR: Please Enter Correct Input!";
                }

	}
	else
	{
		print "Please Enter Correct Input!";
	}
	default2();
}

sub default
{
        print <<END_OF_PRINTING;
	
        <html>
        <head>
        <title>TechnoFile | Administrator</title>
        </head>
        <body>
        <h1>Welcome, Administrator!</h1>
        <h3>To add/remove a rule category, fill in the Category text field a press the 'Add Cat.' or 'Remove Cat.' button</h3>
	<h3>To add/remove a rule subcategory, fill in the Category and Subcategory text fields a press the 'Add Sub.' or 'Remove Sub.' button</h3>

	<form action="adminpage.pl.cgi" method=POST>
        Add Rule Category/Subcategory or Modify Subcategory<br>
	Category:
        <input type="text" name="acat">
        <br>
        Subcategory:
        <input type="text" name="asub">
	Subcategory Weight: <input type="text" name="weight">
        <br>
	Word/Phrase: 
	<input type="text" name="phrase">
	Word/Phrase Weight:
	<input type="text" name="pweight"><br>
        <input type="submit" name="addcat" value="Add Cat.">
	<input type="submit" name="addsub" value="Add Sub.">
	<input type="submit" name="modsub" value="Modify Sub. Weight">
	<input type="submit" name="addphrase" value="Add Phrase">
	<input type="submit" name="modphrase" value="Modify Phrase Weight"></form>
	<br><br>
	<form action="adminpage.pl.cgi" method=POST>
        Remove Rule Category/Subcategory<br>
        Category:
        <input type="text" name="rcat">
        Subcategory:
        <input type="text" name="rsub">
	Word/Phrase:
	<input type="text" name="rphrase">
	<br>
        <input type="submit" name="remcat" value="Remove Cat.">
        <input type="submit" name="remsub" value="Remove Sub.">
	<input type="submit" name="remphrase" value="Remove Phrase"></form>
END_OF_PRINTING
}

sub default2() {
	my ($rule, $subrule, $subruleweight, $phrase, $phraseweight);
	print "<hr><table><tr><td>Ruleset</td><td>Subcategory</td><td>Weight</td><td>Phrase</td><td>Weight</td></tr>";
	open(RULES, "../users/rules/rules.txt") || die "Failed to read rules.txt";
	flock(RULES, 2);
	$rule = <RULES>;
	do {
	    chomp $rule;
	    print "<tr><td>$rule</td>";
	    open(SUBRULES, "../users/rules/$rule/$rule") || die "Failed to read subrule $rule";
	    flock(SUBRULES, 2);
	    $subrule = <SUBRULES>;
	    do {
		chomp $subrule;
		($subruleweight, $subrule) = split(/\t/, $subrule);
		print "<td>$subrule</td><td>$subruleweight</td>";
		open(PHRASES, "../users/rules/$rule/$subrule.txt") || die "Failed to read subcategory $subrule";
		flock(PHRASES, 2);
		$phrase = <PHRASES>;
		do {
		    chomp $phrase;
		    ($phraseweight, $phrase) = split(/\t/, $phrase);
		    print "<td>$phrase</td><td>$phraseweight</td></tr><tr><td></td><td></td><td></td>";
		} while ($phrase = <PHRASES>);
		close(PHRASES);
		print "</tr><tr><td></td>";
	    } while ($subrule = <SUBRULES>);
	    close(SUBRULES);
	    print "</tr><tr>";
	} while ($rule = <RULES>);
	close(RULES);
	
	print "</tr></table></body></html>";
}

